package com.java.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.auth.mapper.SysUserMapper;
import com.java.auth.pojo.LoginUserInfoHelper;
import com.java.auth.service.SysDeptService;
import com.java.auth.service.SysPostService;
import com.java.auth.service.SysUserService;
import com.java.common.constants.UserConstants;
import com.java.common.utils.MD5Util;
import com.java.common.utils.R;
import com.java.model.system.SysDept;
import com.java.model.system.SysPost;
import com.java.model.system.SysUser;
import com.java.vo.system.SysUserQueryVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper,SysUser> implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysPostService sysPostService;


    /**
     * 分页条件查询
     *
     * @param current
     * @param limit
     * @param sysUserQueryVo
     */
    @Override
    public R select(Long current, Long limit, SysUserQueryVo sysUserQueryVo) {
        IPage<SysUser> page = new Page<>(current,limit);
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        String keyword = sysUserQueryVo.getKeyword();
        String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();
        if(!StringUtils.isEmpty(keyword)){
            queryWrapper.like("username",keyword);
        }
        if(!StringUtils.isEmpty(createTimeBegin)){
            queryWrapper.ge("create_time",createTimeBegin);
        }
        if(!StringUtils.isEmpty(createTimeEnd)){
            queryWrapper.le("create_time",createTimeEnd);
        }
        page = sysUserMapper.selectPage(page, queryWrapper);
        return R.ok(page);

    }

    /**
     * 根据id查找用户
     *
     * @param id
     * @return
     */
    @Override
    public R selectById(Long id) {

        SysUser sysUser = sysUserMapper.selectById(id);
        if(sysUser!=null){
            return R.ok(sysUser);
        }
        return R.fail().message("用户查询失败");
    }

    /**
     * 保存用户
     *
     * @param sysUser
     * @return
     */
    @Override
    public R saveUser(SysUser sysUser) {
        sysUser.setPassword(MD5Util.encode(sysUser.getPassword()+ UserConstants.USER_SLAT));
        int rows = sysUserMapper.insert(sysUser);
        if(rows==0){
            return R.fail().message("添加失败！");
        }
        return R.ok().message("添加成功！");
    }

    @Override
    public R update(SysUser sysUser) {
        int rows = sysUserMapper.updateById(sysUser);
        if(rows==0){
            return R.fail().message("更新失败！");
        }
        return R.ok().message("更新成功！");
    }

    /**
     * 根据id删除用户
     *
     * @param id
     * @return
     */
    @Override
    public R deleteById(Long id) {
        int rows = sysUserMapper.deleteById(id);
        if(rows==0){
            return R.fail().message("删除失败！");
        }
        return R.ok().message("删除成功！");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public R deleteBatch(List<Long> ids) {
        int rows = sysUserMapper.deleteBatchIds(ids);
        if(rows==0){
            return R.fail().message("批量删除失败！");
        }
        return R.ok().message("批量删除成功！");
    }

    /**
     * 更新用户状态
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public R updateStatus(Long id, Integer status) {
        SysUser sysUser = sysUserMapper.selectById(id);
        sysUser.setStatus(status);
        int rows = sysUserMapper.updateById(sysUser);
        if(rows==0){
            return R.fail().message("状态修改失败！");
        }
        return R.ok().message("状态修改成功！");
    }

    @Override
    public SysUser getByUsername(String username) {

        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUser::getUsername,username);
        SysUser sysUser = sysUserMapper.selectOne(lambdaQueryWrapper);

        return sysUser;
    }

    /**
     * 我的信息
     * @return
     */
    @Override
    public R getCurrentUser() {
        SysUser sysUser = sysUserMapper.selectById(LoginUserInfoHelper.getUserId());
        SysDept sysDept = sysDeptService.getById(sysUser.getDeptId());
        SysPost sysPost = sysPostService.getById(sysUser.getPostId());
        Map<String, Object> map = new HashMap<>();
        map.put("name", sysUser.getName());
        map.put("phone", sysUser.getPhone());
        map.put("deptName", sysDept.getName());
        map.put("postName", sysPost.getName());
        return R.ok(map);
    }
}
